<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

// 为方便系统升级，二次开发中用到的公共函数请写在此文件，禁止修改common.php文件
// ===== 系统升级时此文件永远不会被覆盖 =====
use League\Csv\Reader;
use League\Csv\CannotInsertRecord;
use League\Csv\Writer;
use think\facade\Request;

/**
 * 二维数组指定一个或多个键来分组数据
 * @author Lj
 * @param array $data 数据源（二维数组）[['name'=>a,'value'=>1,'remork'=001],['name'=>a,'value'=>2,'remork'=001]]
 * @param array $groupKey 需要根据指定的键名进行分组的数据（一维数组）['name','value']
 * @return bool|array [失败返回false 成功返回对应数组]
 * @date 2020-3-18
 */
if (!function_exists('array_more_group')){
    function array_more_group($data , $groupKey = []){
        if(empty($data) || empty($groupKey) || !is_array($groupKey) || !is_array($data)){
            return false;
        }
        $newData = [];
        array_values($groupKey);
        foreach ($data as $k=>$v){
           $key = '';
           foreach ($groupKey as $ka=>$va){
               if(isset($data[$k][$va])){
                   $key .= count($groupKey) == 1 || $ka == count($groupKey)-1 ? $v[$va] : $v[$va].'_';
               }
           }
           if(isset($newData[$key])){

               array_push($newData[$key],$v);
           }else{
               $newData[$key][] = $v;
           }
        }
        return $newData;
    }
}


if(!function_exists('create_order_sn')){
    /**
     *  创建订单号
     */
    function create_order_sn() {
        $arr = range('A','Z');
        return  $arr[mt_rand(0,25)] . date('YmdHis',time()) . mt_rand(1000,9999);
    }
}



if(!function_exists('export_csv')){
    /**
     *  导出csv
     */
    function export_csv($top=[],$data=[],$type=''){
        $fileName =  'csv/'.$type.DIRECTORY_SEPARATOR.time().'.csv';
        $json = ['code'=>0,'msg'=>''];
        try {
              $writer = Writer::createFromPath('./'.$fileName, 'w+');
              foreach ($top as $k => $v) {
                  $top[$k] = iconv("UTF-8", "GB2312//IGNORE", $v);
              }
              foreach ($data as $k => $v) {
                  $data[$k]['nick'] = iconv("UTF-8", "GB2312//IGNORE", $v['nick']);
                  $data[$k]['sb_card'] = iconv("UTF-8", "GB2312//IGNORE", $v['sb_card']);
                  $data[$k]['sex'] = iconv("UTF-8", "GB2312//IGNORE", $v['sex']);
                  $data[$k]['shop_id'] = iconv("UTF-8", "GB2312//IGNORE", $v['shop_id']);
              }
              $writer->insertOne($top);
              $writer->insertAll($data); //using an array
        } catch (CannotInsertRecord $e) {
              $json['msg'] = $e->getRecords();
              return json($json);
        }
        $json['code'] = 200;
        $json['msg'] = Request::domain() .DIRECTORY_SEPARATOR.$fileName;
        return json($json);
    }
}
